﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Cmd;
using DG.Tweening;
using GameData;
using UnityEngine.UI;

public class ManagerUI : MonoBehaviour
{

    public GameObject infoObj;
    public GameObject countObj;

    public Text PeopleText;
    public Text NumberText;
    public Text ShowText;
    public Text RecText;
    public Text PickText;
    public Text DueText;
    public Text ComText;
    public Text CanlText;
    public Text PickNumText;

    public GameObject infoBtn;
    public GameObject countBtn;
    public GameObject backBtn;

    public GameObject RecNumBtn;

    public GameObject tipObj;
    public GameObject recTip;
    public GameObject okbtn;
    public GameObject canbtn;

    //pickTip
    public GameObject pickobj;
    public GameObject picktipBtn;
    public Text pickText;
    //
    public GameObject scrollObj;
    public GameObject numPre;

    //设置
    public GameObject setBtn;
    public GameObject setbackBtn;
    public GameObject setObj;
    //添加数量
    public GameObject addnumBtn;
    public InputField AddnumInputField;
    //设置一次抽取个数
    public GameObject setpicknumBtn;
    public InputField setpicknumField;
    //清空数据
    public GameObject clearBtn;
    public GameObject cleanObj;

    //返回
    public GameObject vipbackBtn;
    public GameObject vipObj;

    //vip input
    public GameObject vipinputObj;
    public GameObject vipinputbackBtn;

    //帐号
    public GameObject InfoBtn;
    public GameObject adminInfoObj;

    //语种
    public ToggleGroup ToggleGroup;
    public Toggle PutongToggle;
    public Toggle YueyuToggle;

    //取号方式
    public Toggle IsReToggle;

    //叫号界面
    public GameObject salecallBtn;
    public SaleUI SaleUi;

    //
    public GameObject customerBtn;
    public GameObject customerInfoUI;
    public CustomerInfo CustomerInfo;

    //批次
    public SetUI setUI;

    // Use this for initialization
    void Start ()
    {
        Messenger<NumAllInfo>.AddListener("SC统计数据", OnNumberInfo);
        Messenger<Dictionary<string, string>>.AddListener("SC设置数据", OnSetDataInfo);
        //发送统计数据
        Cmd.ReceiveCountSend rcs = new ReceiveCountSend();
        rcs.Send(GlobalVO.GetInstance().roleInfo.UserInfo, OnNumberInfo);

        EventTriggerListener.Get(infoBtn).onClick = OnInfo;
        EventTriggerListener.Get(countBtn).onClick = OnCount;
        EventTriggerListener.Get(backBtn).onClick = OnBack;
        EventTriggerListener.Get(RecNumBtn).onClick = OnRecNum;
        EventTriggerListener.Get(okbtn).onClick = OnRecOK;
        EventTriggerListener.Get(canbtn).onClick = Oncanbtn;
        EventTriggerListener.Get(picktipBtn).onClick = Oncl;
        EventTriggerListener.Get(setBtn).onClick = Onset;
        EventTriggerListener.Get(setbackBtn).onClick = Onsetback;
        EventTriggerListener.Get(addnumBtn).onClick = OnaddnumBtn;
        EventTriggerListener.Get(setpicknumBtn).onClick = OnsetpicknumBtn;
        EventTriggerListener.Get(vipbackBtn).onClick = OnvipbackBtn;
        EventTriggerListener.Get(clearBtn).onClick = OnClear;
        EventTriggerListener.Get(vipinputbackBtn).onClick = OnvipinputBtn;
        EventTriggerListener.Get(InfoBtn).onClick = OnInfoBtn;
        EventTriggerListener.Get(salecallBtn).onClick = OnsalecallBtn;
        EventTriggerListener.Get(customerBtn).onClick = OncustomerBtn;


        Screen.sleepTimeout = SleepTimeout.NeverSleep;

        GetSoundType getSoundType = new GetSoundType();
        getSoundType.Send(GlobalVO.GetInstance().roleInfo.UserInfo, SetSoundType);

        GetRecNumType getRecNumType = new GetRecNumType();
        getRecNumType.Send(GlobalVO.GetInstance().roleInfo.UserInfo, OnGetRecNum);

        PutongToggle.onValueChanged.AddListener(OnChangeTo);
        YueyuToggle.onValueChanged.AddListener(OnYYChangTo);

        CustomerInfo.isShowNum = true;


    }


    void OnGetRecNum(RecNumType recNumType)
    {
        GlobalVO.GetInstance().NumAllInfo.isRecIdCard = recNumType.reNumType;

    }


    void OnIsReChangeTo(bool rec)
    {
        SetRecType SetRecType = new SetRecType();
        RecNumType recNumType = new RecNumType();
        recNumType.reNumType = rec;
        SetRecType.Send(recNumType, OnRespnseRec);


    }

    void OnRespnseRec(RecNumType rec)
    {
        GlobalVO.GetInstance().NumAllInfo.isRecIdCard = rec.reNumType;
    }

    void OnChangeTo(bool res)
    {
        //
        if (res)
        {
            SoundType soundType = new SoundType();
            soundType.putonghua = true;
            soundType.yueyu = false;
            SetSoundType soundTypeSend = new SetSoundType();
            soundTypeSend.Send(soundType, SetSoundType);
        }

    }

    void OnYYChangTo(bool res)
    {
        if (res)
        {
            SoundType soundType = new SoundType();
            soundType.putonghua = false;
            soundType.yueyu = true;
            SetSoundType soundTypeSend = new SetSoundType();
            soundTypeSend.Send(soundType, SetSoundType);
        }
    }


    public void SetSoundType(SoundType soundType)
    {
        GlobalVO.GetInstance().SoundType = soundType;
        

    }

    void OnClear(GameObject obj)
    {
        //情况数据
        //
        cleanObj.SetActive(true);
        cleanObj.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = cleanObj.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);


    }


    void OnRecOK(GameObject obj)
    {
        PickNumSend pickNumSend = new PickNumSend();
        pickNumSend.Send(GlobalVO.GetInstance().roleInfo.UserInfo, OnClickPickNum);

    }

    void OnvipbackBtn(GameObject obj)
    {
        vipObj.SetActive(true);
        vipObj.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = vipObj.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);
        VipView vipView = vipObj.GetComponent<VipView>();
        vipView.OnShow();
    }

    void OnvipinputBtn(GameObject obj)
    {
        //
        vipinputObj.SetActive(true);
        vipinputObj.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = vipinputObj.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);
        InputVipView vipView = vipinputObj.GetComponent<InputVipView>();
        vipView.OnShow();

    }

    void OnsalecallBtn(GameObject obj)
    {
        SaleUi.gameObject.SetActive(true);
        SaleUi.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = SaleUi.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);

    }

    void OncustomerBtn(GameObject obj)
    {
        customerInfoUI.gameObject.SetActive(true);
        customerInfoUI.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = customerInfoUI.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);

    }

    void OnInfoBtn(GameObject obj)
    {
        adminInfoObj.SetActive(true);
        adminInfoObj.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = adminInfoObj.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);

    }

    public void OnClickPickNum(PickNumInfo pickNumInfo)
    {
        recTip.SetActive(false);
        pickobj.SetActive(true);
        pickobj.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = pickobj.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);
        //
        string str = "";
        string pla = ",";
        for (int i = 0; i < pickNumInfo.NumberInfoList.Count; i++)
        {
            pla = ", ";
            if (i== pickNumInfo.NumberInfoList.Count-1)
            {
                pla = "";
            }
            str += pickNumInfo.NumberInfoList[i].value+ pla;
        }
        pickText.text = str;
    }


    void OnRecNum(GameObject obj)
    {
        tipObj.SetActive(true);
        recTip.SetActive(true);
        pickobj.SetActive(false);
        recTip.transform.localScale = new Vector3(0,0,0);
        Tweener tw = recTip.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);

    }

    void Oncanbtn(GameObject obj)
    {
        recTip.transform.localScale = new Vector3(1, 1, 1);
        Tweener tw = recTip.transform.DOScale(new Vector3(0, 0, 0), 0.3f).SetEase(Ease.OutBack);
        tw.OnComplete(Oncl);
    }

    void Oncl(GameObject obj)
    {
        Oncl();

    }
    void Oncl()
    {
        tipObj.SetActive(false);
        recTip.SetActive(false);
        pickobj.SetActive(false);
        recTip.transform.localScale = new Vector3(0, 0, 0);
        pickobj.transform.localScale = new Vector3(0, 0, 0);

    }


    void Onset(GameObject obj)
    {
        //设置
        setObj.SetActive(true);
        setObj.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = setObj.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);
        //



    }

    void Onsetback(GameObject obj)
    {
        setObj.transform.localScale = new Vector3(1, 1, 1);
        Tweener tw = setObj.transform.DOScale(new Vector3(0, 0, 0), 0.3f).SetEase(Ease.OutBack);
        tw.OnComplete(() =>
        {
            setObj.SetActive(false);
        });
    }


    void OnaddnumBtn(GameObject obj)
    {
        //添加
        int num = int.Parse(AddnumInputField.text);
        if (num>0)
        {
            NumberData numberData = new NumberData();
            numberData.value = num;
            AddCurlNumberSend addCurlNumber = new AddCurlNumberSend();
            addCurlNumber.Send(numberData, addcurnumRes);
        }

    }


    void OnsetpicknumBtn(GameObject obj)
    {
        //添加
        int num = int.Parse(setpicknumField.text);
        if (num > 0)
        {
            NumberData numberData = new NumberData();
            numberData.value = num;
            SetOncePickNumSend setOncePickNumSend = new SetOncePickNumSend();
            setOncePickNumSend.Send(numberData, addcurnumRes);
        }

    }

    void addcurnumRes(NumAllInfo numAllInfo)
    {
        

    }


    void OnBack(GameObject obj)
    {
        OnRemoveListener();
        Application.LoadLevel("Main");
    }

    public void OnRemoveListener()
    {
        Messenger<NumAllInfo>.RemoveListener("SC统计数据", OnNumberInfo);
        Messenger<Dictionary<string, string>>.RemoveListener("SC设置数据", OnSetDataInfo);
        SaleUi.RemoveListener();
        CustomerInfo.OnRemoveListener();
    }


    void OnInfo(GameObject obj)
    {
        countObj.transform.localScale = new Vector3(0,0,0);
        countObj.SetActive(true);
        Tweener tw = countObj.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);

        infoObj.transform.localScale = new Vector3(1, 1, 1);
        infoObj.SetActive(true);
         tw = infoObj.transform.DOScale(new Vector3(0, 0, 0), 0.3f).SetEase(Ease.OutBack);

       //设置数据
       GetAllNumberSend getAllNumberSend = new GetAllNumberSend();
       getAllNumberSend.Send(GlobalVO.GetInstance().roleInfo.UserInfo, OnUpdateAllNumView);


    }


    void OnUpdateAllNumView(AllNumbers allNumbers)
    {
        //更新
        int childCount = scrollObj.transform.childCount;
        int numCount = allNumbers.NumberInfoList.Count;
        int vnck = Math.Max(childCount, numCount);
        for (int i = 0; i < vnck; i++)
        {

            if (i < numCount)
            {
                Text text;
                if (i < childCount)
                {
                    Transform objtf = scrollObj.transform.GetChild(i);
                    objtf.gameObject.SetActive(true);
                    text = objtf.FindChild("Text").GetComponent<Text>();

                }
                else
                {
                    GameObject obj = GameObject.Instantiate(numPre);
                    obj.transform.SetParent(scrollObj.transform);
                    obj.transform.SetAsLastSibling();
                    text = obj.transform.FindChild("Text").GetComponent<Text>();
                }
                //
                NumberInfo numberInfo = allNumbers.NumberInfoList[i];
                text.text = numberInfo.name+"号码：" + numberInfo.groupTag+ numberInfo.value + "  排:" + numberInfo.QueueNum+"位"+" "+ numberInfo.name;
            }
            else
            {
                Transform objtf = scrollObj.transform.GetChild(i);
                objtf.gameObject.SetActive(false);

            }



        }

        GridLayoutGroup gridLayoutGroup = scrollObj.GetComponent<GridLayoutGroup>();
        RectTransform rec = scrollObj.GetComponent<RectTransform>();
        //设置高度
        int callheight = (int)Math.Ceiling(numCount /3.0f);
        float newheight = callheight*gridLayoutGroup.cellSize.y+50;
        rec.sizeDelta = new Vector2(rec.sizeDelta.x,newheight);



    }

    void OnCount(GameObject obj)
    {
        countObj.transform.localScale = new Vector3(1, 1, 1);
        countObj.SetActive(true);
        Tweener tw = countObj.transform.DOScale(new Vector3(0, 0, 0), 0.3f).SetEase(Ease.OutBack);

        infoObj.transform.localScale = new Vector3(0, 0, 0);
        infoObj.SetActive(true);
        tw = infoObj.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);
    }


    void OnSetData()
    {
        PeopleText.text = "实际人数："+GlobalVO.GetInstance().NumAllInfo.peopleCount.ToString();
        NumberText.text = "当前号码：" + GlobalVO.GetInstance().NumAllInfo.CurCount;
        ShowText.text = "可显示数：" + GlobalVO.GetInstance().NumAllInfo.showNum;
        RecText.text = "领取数：" + GlobalVO.GetInstance().NumAllInfo.recCount;
        PickText.text = "抽中数：" + GlobalVO.GetInstance().NumAllInfo.pickCount;
        DueText.text = "处理中数：" + GlobalVO.GetInstance().NumAllInfo.duCount;
        ComText.text = "完成数：" + GlobalVO.GetInstance().NumAllInfo.completeCount;
        CanlText.text = "放弃数：" + GlobalVO.GetInstance().NumAllInfo.cancleCount;
        PickNumText.text = "一次抽取数：" + GlobalVO.GetInstance().NumAllInfo.PickNum;

    }

    public bool isInitBol = false;

    void OnNumberInfo(NumAllInfo numallInfo)
    {
        GlobalVO.GetInstance().NumAllInfo = numallInfo;
        if (!isInitBol)
        {
            isInitBol = true;
            IsReToggle.isOn = GlobalVO.GetInstance().NumAllInfo.isRecIdCard;
            IsReToggle.onValueChanged.AddListener(OnIsReChangeTo);
        }

        OnSetData();
    }

    void OnSetDataInfo(Dictionary<string, string> dic)
    {
        if (dic.ContainsKey("批次"))
        {
            setUI.SetOn(dic["批次"]);
        }
        if (dic.ContainsKey("连号"))
        {
            setUI.SetLineNum(dic["连号"]);
        }
    }


    // Update is called once per frame
    void Update () {
	
	}
}
